<template>
    <div class="app-container">
        <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
            <el-form-item label="姓名">
                <el-input v-model="queryParams.name"></el-input>
            </el-form-item>
            <el-form-item label="地址">
                <el-input v-model="queryParams.address"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        icon="el-icon-plus"
                        @click="handleAdd"
                >新增
                </el-button>
            </el-col>
            <right-toolbar @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
                v-loading="loading"
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
                    type="index"
                    label="行序号"
                    width="100"
                    :index="1">
            </el-table-column>
            <el-table-column
                    prop="date"
                    label="日期"
                    :formatter="dateFormat"
                    width="280">

            </el-table-column>
            <el-table-column
                    prop="name"
                    label="姓名"
                    width="280">
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="地址">
            </el-table-column>

            <el-table-column
                    fixed="right"
                    label="操作"
                    width="350">
                <template #default="scope">
                    <el-button type="primary" @click="handleView(scope.row)"   size="small">查看</el-button>
                    <el-button  @click="handleUpdate(scope.row)" size="small">编辑</el-button>
                    <el-button  type="danger" @click="handleDelete(scope.row)" size="small">删除</el-button>
                    <el-button type="text"   size="small">文本按钮</el-button>
                </template>
            </el-table-column>
        </el-table>


        <el-dialog :title="title" width="30%" v-model="dialogFormVisible">
            <el-form :model="dialogForm" ref="dialogForm">
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="dialogForm.name"></el-input>
                </el-form-item>
                <el-form-item label="地址" :label-width="formLabelWidth">
                    <el-input v-model="dialogForm.address"></el-input>
                </el-form-item>
                <el-form-item label="日期" :label-width="formLabelWidth">
                    <el-date-picker type="date" placeholder="选择日期"
                                    v-model="dialogForm.date"
                                    style="width: 100%;"></el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
        </el-dialog>

        <div class="pagination">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryParams.pageNum"
                    :page-sizes="[5,10, 20, 30, 40]"
                    :page-size="queryParams.pageSize"
                    layout="total,sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>

    import {getTableData, getUser, updateUser, addUser, delUser} from '../api/index'   //路径
    import moment from 'moment'

    export default {
        name: "basetable",
        data() {
            return {
                formLabelWidth: '120px',
                // 是否显示弹出层,增加
                dialogFormVisible: false,
                // 弹出层标题
                title: "",
                loading: true,
                // 总条数
                total: 0,
                tableData: [],
                dialogForm: {
                    id: '',
                    name: '',
                    address: '',
                    date: ''
                },
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    name: undefined,
                    address: undefined
                },

                // 选中数组
                ids: [],
            };
        },
        created() {
            this.getList();
        },
        methods: {

            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids
                const name = row.name
                this.$confirm('是否确认删除名字为"' + name + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return delUser(ids);
                }).then((response) => {
                        this.getList();
                        if (response.code == 0) {
                            this.$message.success(response.msg);
                        } else {
                            this.$message.error(response.msg);
                        }
                    }
                )
            },

            // 取消按钮
            cancel() {
                this.dialogFormVisible = false;
                this.reset();
            },
            // ** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.dialogFormVisible = true;
                this.title = "添加用户";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.dialogFormVisible = true;
                const id = row.id || this.id;
                getUser(id).then(response => {
                    this.dialogForm = response.data;
                    this.title = "修改用户";
                    this.dialogFormVisible = true;
                });
            },
            /** 提交按钮 */
            submitForm: function () {
                this.$refs["dialogForm"].validate(valid => {
                    if (valid) {
                        if (this.dialogForm.id != undefined) {
                            updateUser(this.dialogForm).then(response => {
                                if (response.code == 0) {
                                    this.$message.success(response.msg);
                                } else {
                                    this.$message.error(response.msg);
                                }
                                this.dialogFormVisible = false;
                                this.getList();
                            });
                        } else {
                            addUser(this.dialogForm).then(response => {
                                if (response.code == 0) {
                                    this.$message.success(response.msg);
                                } else {
                                    this.$message.error(response.msg);
                                }
                                this.dialogFormVisible = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.noticeId)
                this.single = selection.length != 1
                this.multiple = !selection.length
            },
            resetForm: function (refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
// 表单重置
            reset() {
                this.dialogForm = {
                    id: undefined,
                    name: undefined,
                    address: undefined,
                    date: undefined,
                };
                this.resetForm("dialogForm");
            },

            dateFormat: function (row, column) {
                var date = row[column.property];
                if (date == undefined) {
                    return ''
                }

                // return moment(date).format("YYYY-MM-DD HH:mm:ss")
                return moment(date).format("YYYY-MM-DD")

            },
            getList() {
                this.loading = true;
                getTableData(this.queryParams).then((res) => {
                    this.tableData = res.rows;
                    this.total = res.total;
                    this.loading = false;
                })

            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            handleSizeChange(val) {
                this.queryParams.pageSize = val;
                this.getList();//根据用户获取的每页显示页面数量显示页面
            },

            handleCurrentChange(val) {
                this.queryParams.pageNum = val;
                this.getList();//获取用户点击的当前页后刷新页面数据
            }
        },

    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>